/*
 * author      : dormi330 
 * date        : 2018/7/19
 * description : TODO
 */

package com.deep.trade.application.history;

import com.deep.trade.application.EventHub;
import com.deep.trade.common.CommonUtils;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class OrderHistoryService {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private EventHub eventHub;

    @PostConstruct
    public void initRegister() {
        logger.info("{}}", CommonUtils.currentClassAndMethod());

        eventHub.registerEvent("EVENT_TREAD_ORDER_FINISHED", msg -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            logger.info("now handle event EVENT_TREAD_ORDER_FINISHED, {}", msg.uglyString());

            String body = msg.getBody();

            try {
                JsonObject jsonObject = CommonUtils.fromString(body);
                System.out.println(((JsonObject)jsonObject.get("outDtoTradeOrder")).get("tenantCode"));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
    }
}
